package com.sys.basics.controller;

import com.sys.basics.config.annotation.SysLogAnnotation;
import lombok.extern.slf4j.Slf4j;
import com.sys.basics.config.jsonRes.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sys.basics.entity.SysArea;
import com.sys.basics.service.SysAreaService;
import io.swagger.annotations.*;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;

/**
 * @author: ares
 * @date: 2021-10-08
 * @description: 行政区域表 前端控制器
 */
@Validated
@Slf4j
@Api(tags = "行政区域管理")
@RestController
@RequestMapping("/basics/sysArea")
public class SysAreaController extends AbstractController<SysAreaService, SysArea> {
    private JsonResult<List<SysArea>> listResult = new JsonResult<>();

    @Autowired
    SysAreaService sysAreaService;

    @SysLogAnnotation("根据code查询tree")
    @ApiOperation("根据code查询tree")
    @ApiImplicitParam(name = "areaCode", value = "区域code", required = true, defaultValue = "310000", paramType = "query")
    @GetMapping("/getTreeByCode")
    public JsonResult<SysArea> getTreeByCode(@NotEmpty(message = "区域code不能为空") String areaCode) {
        return result.success("区域tree查询成功", sysAreaService.getTreeByCode(areaCode));
    }

    @SysLogAnnotation("根据父节点code查询tree")
    @ApiOperation("根据父节点code查询tree")
    @ApiImplicitParam(name = "parentCode", value = "父节点code", defaultValue = "310000", required = true, paramType = "query")
    @GetMapping("/getChildrenTreeByParentCode")
    public JsonResult<List<SysArea>> getChildrenTreeByParentCode(@NotEmpty(message = "父区域code不能为空") String parentCode) {
        return listResult.success("区域tree查询成功", sysAreaService.getChildrenTreeByParentCode(parentCode));
    }

    @SysLogAnnotation("根据父节点code查询子区域")
    @ApiOperation("根据父节点code查询子区域")
    @ApiImplicitParam(name = "parentCode", value = "父节点code", defaultValue = "0", required = true, paramType = "query")
    @GetMapping("/getChildrenByParentCode")
    public JsonResult<List<SysArea>> getChildrenByParentCode(@NotEmpty(message = "父区域code不能为空") String parentCode) {
        return listResult.success("区域tree查询成功", sysAreaService.getChildrenByParentCode(parentCode));
    }

    @SysLogAnnotation("根据子节点code查询上级tree")
    @ApiOperation("根据子节点code查询上级tree")
    @ApiImplicitParam(name = "areaCode", value = "子节点的code", defaultValue = "310114", required = true, paramType = "query")
    @GetMapping("/getTreeByCodeAsLeaf")
    public JsonResult<SysArea> getTreeByCodeAsLeaf(@NotEmpty(message = "子区域code不能为空") String areaCode) {
        return result.success("区域tree查询成功", sysAreaService.getTreeByCodeAsLeaf(areaCode));
    }

    @SysLogAnnotation("根据code删除自身及子节点")
    @ApiOperation("根据code删除自身及子节点")
    @ApiImplicitParam(name = "areaCode", value = "子节点的code", required = true, paramType = "query")
    @DeleteMapping("/removeByCode")
    public JsonResult removeByCode(@NotEmpty(message = "区域code不能为空") String areaCode) {
        boolean removed = sysAreaService.removeSelfAndChildrenByCode(areaCode);
        if (removed) {
            result.success("删除成功");
        } else {
            result.error("删除失败");
        }
        return result;
    }
}
